package org.jeecg.modules.wlhy.hytaxsalesinvoice.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.third.dongjiang.tax.system.dto.ApiBaseResDTO;
import org.jeecg.modules.wlhy.hytaxsalesinvoice.entity.HyTaxSalesinvoice;
import org.jeecg.modules.wlhy.hytaxsalesinvoice.service.IHyTaxSalesinvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: hy_tax_salesinvoice
 * @Author: duanlikao
 * @Date:   2021-11-22
 * @Version: V1.0
 */
@Api(tags="hy_tax_salesinvoice")
@RestController
@RequestMapping("/hytaxsalesinvoice/hyTaxSalesinvoice")
@Slf4j
public class HyTaxSalesinvoiceController extends JeecgController<HyTaxSalesinvoice, IHyTaxSalesinvoiceService> {
	@Autowired
	private IHyTaxSalesinvoiceService hyTaxSalesinvoiceService;

	/**
	 * 分页列表查询
	 *
	 * @param hyTaxSalesinvoice
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="hy_tax_salesinvoice-分页列表查询", notes="hy_tax_salesinvoice-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyTaxSalesinvoice hyTaxSalesinvoice,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyTaxSalesinvoice> queryWrapper = QueryGenerator.initQueryWrapper(hyTaxSalesinvoice, req.getParameterMap());
		queryWrapper.groupBy("fp_id");
		Page<HyTaxSalesinvoice> page = new Page<HyTaxSalesinvoice>(pageNo, pageSize);
		IPage<HyTaxSalesinvoice> pageList = hyTaxSalesinvoiceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hyTaxSalesinvoice
	 * @return
	 */
	@AutoLog(value = "hy_tax_salesinvoice-添加")
	@ApiOperation(value="hy_tax_salesinvoice-添加", notes="hy_tax_salesinvoice-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyTaxSalesinvoice hyTaxSalesinvoice) {
		hyTaxSalesinvoiceService.save(hyTaxSalesinvoice);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hyTaxSalesinvoice
	 * @return
	 */
	@AutoLog(value = "hy_tax_salesinvoice-编辑")
	@ApiOperation(value="hy_tax_salesinvoice-编辑", notes="hy_tax_salesinvoice-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyTaxSalesinvoice hyTaxSalesinvoice) {
		hyTaxSalesinvoiceService.updateById(hyTaxSalesinvoice);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "hy_tax_salesinvoice-通过id删除")
	@ApiOperation(value="hy_tax_salesinvoice-通过id删除", notes="hy_tax_salesinvoice-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hyTaxSalesinvoiceService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "hy_tax_salesinvoice-批量删除")
	@ApiOperation(value="hy_tax_salesinvoice-批量删除", notes="hy_tax_salesinvoice-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyTaxSalesinvoiceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_tax_salesinvoice-通过id查询", notes="hy_tax_salesinvoice-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyTaxSalesinvoice hyTaxSalesinvoice = hyTaxSalesinvoiceService.getById(id);
		if(hyTaxSalesinvoice==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyTaxSalesinvoice);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyTaxSalesinvoice
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyTaxSalesinvoice hyTaxSalesinvoice) {
        return super.exportXls(request, hyTaxSalesinvoice, HyTaxSalesinvoice.class, "hy_tax_salesinvoice");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyTaxSalesinvoice.class);
    }

	 @AutoLog(value = "增值税销项发票信息-批量上报")
	 @ApiOperation(value="增值税销项发票信息-批量上报", notes="增值税销项发票信息-批量上报")
	 @PutMapping(value = "/taxSalesinvoiceBatch")
	 public Result<?> taxSalesinvoiceBatch(@RequestBody JSONObject jSONObject) {
		 String ids = jSONObject.getString("ids");
		 List <String> idStrings =  Arrays.asList(ids.split(","));
		 QueryWrapper<HyTaxSalesinvoice> queryWrapper = new QueryWrapper();
		 queryWrapper.in("id",idStrings);
		 List<HyTaxSalesinvoice> hyTaxSalesinvoices =  hyTaxSalesinvoiceService.list(queryWrapper);

		 List<String> fpIds  =  hyTaxSalesinvoices.stream().map( item -> item.getFpId() ).distinct()
				 .collect(Collectors.toList());

		 for (String fpId : fpIds){
			 hyTaxSalesinvoiceService.uploadTax( fpId);
		 }
		 return Result.OK("上报成功!");
	 }



}
